<template>
  <div class="login">
    <div class="login_inner">
      <div class="login_pic">
        <p class="welcome">Welcome</p>
      </div>
      <div class="login_form">
        <p class="form_title">{{ title }}</p>
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="form"
        >
          <el-form-item prop="username">
            <div class="username">
              <el-input v-model="loginForm.username" placeholder="账号">
                <i slot="prefix" class="el-icon-s-custom" />
              </el-input>
            </div>
          </el-form-item>
          <el-form-item prop="password">
            <div class="password">
              <el-input
                v-model="loginForm.password"
                show-password
                placeholder="密码"
                @keyup.enter.native="login"
              >
                <i slot="prefix" class="iconfont icon-password" />
              </el-input>
            </div>
          </el-form-item>
          <el-form-item>
            <div div class="login_btn" @click="login">
              <i v-show="loading" class="el-icon-loading" />
              登录
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { validUsername, validPassword } from '@/utils'
const { name } = require('@/config/ProjectTitle')
export default {
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    var validatePassword = (rule, value, callback) => {
      if (!validPassword(value)) {
        callback(
          new Error('密码必须包含大小写字母、数字和特殊字符（长度大于6位）！')
        )
      }
      callback()
    }
    return {
      loading: false,
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ]
      },
      title: name
    }
  },
  methods: {
    login() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: '/' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  width: 100vw;
  height: 100vh;
  background: $black_light;
  vertical-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  &_inner {
    width: 1591px;
    height: 902px;
    background: $black_light;
    margin: 0 auto;
    border-radius: 10px;
    display: flex;
    justify-content: center;
    transform: scale(0.9);
    .login_pic {
      flex: 1;
      height: 100%;
      background: $black_light;
      background: url('~@/assets/login_bg.png');
      position: relative;
      .welcome {
        font-size: 100px;
        color: #fff;
        position: absolute;
        left: 60px;
        bottom: 56px;
        font-weight: 300;
      }
    }
    .login_form {
      width: 563px;
      height: 100%;
      flex-shrink: 0;
      background: #fff;
      border-top-right-radius: 10px;
      border-bottom-right-radius: 10px;
      padding: 105px 79px;
      .form_title {
        font-size: 48px;
        font-weight: 400;
        color: #333333;
        line-height: 120px;
        text-align: center;
      }
      .form {
        margin-top: 92px;
        .el-form-item {
          margin-bottom: 70px;
        }
        .username {
          height: 64px;
          font-size: 20px;
          color: #999999;
          line-height: 64px;
          border-bottom: 1px solid #e0e0e0;
        }
        .password {
          height: 64px;
          font-size: 20px;
          color: #999999;
          line-height: 64px;
          border-bottom: 1px solid #e0e0e0;
        }
        ::v-deep {
          .el-input__inner {
            height: 50px;
            border: none;
            font-size: 18px;
          }
        }
        .login_btn {
          width: 350px;
          height: 60px;
          background: #ff7e00;
          border-radius: 4px;
          box-shadow: 4px 18px 26px 0px rgba(15, 53, 80, 0.26);
          font-size: 22px;
          text-align: center;
          color: #ffffff;
          line-height: 60px;
          margin: 0 auto;
          margin-top: 120px;
          cursor: pointer;
          &:hover {
            opacity: 0.9;
          }
        }
      }
    }
  }
}
</style>
